package com.my.stock.controller;

import com.my.common.JsonUtil;
import com.my.stock.entity.Stock;
import com.my.stock.service.StockService;
import com.my.stockcomeenter.entity.StockComeEnter;
import com.my.stockcomeenter.service.StockComeEnterService;
import com.my.supplier.entity.Supplier;
import com.my.supplier.service.SupplierService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "*")
public class StockController {
	// 引入stockservice
	@Autowired
	private StockService stockService;
	// 引入stockComeEnterService
	@Autowired
	private StockComeEnterService stockComeEnterService;

	// 查询stock
	@RequestMapping("getStockById")
	public String getSupplicer(String id) {

		return JsonUtil.success(stockService.getStockById(Long.valueOf(id)));
	}

	// 添加stock
	@RequestMapping("addStock")
	public String addSupplicer(Stock stock) {
		stockService.addStock(stock);
		return JsonUtil.success();
	}

	// 关联查询stock
	@RequestMapping("getStockSupplier")
	public String getStockSupplier(String sku, String supCoding, String orname, String supName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sku", sku);
		map.put("supCoding", supCoding);
		map.put("orname", orname);
		map.put("supName", supName);
		return JsonUtil.success(stockService.getStockSupplier(map));
	}

	// 商品的出入库
	@RequestMapping("comeEnterStock")
	public String comeEnterStock(String sid, StockComeEnter stockComeEnter) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("************************************" + sid);
		System.out.println(stockComeEnter);
		map.put("id", sid);
		map.put("cENum", stockComeEnter.getcENum());
		map.put("type", stockComeEnter.getComeEnterType());
		int i = stockService.updateNum(map);
		if (i != 0) {
			stockComeEnterService.addStockComeEnter(stockComeEnter);
		}
		return JsonUtil.success();
	}

	/* 当发货数量和锁定数量相等的时候 */
	@RequestMapping("sendStock")
	public String sendStock(String sid, StockComeEnter stockComeEnter) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("====================================" + sid);
		stockComeEnter.setComeEnterType(5);
		System.out.println(stockComeEnter);
		map.put("id", sid);
		map.put("cENum", stockComeEnter.getcENum());
		map.put("type", stockComeEnter.getComeEnterType());
		int i = stockService.updateNum(map);
		if (i != 0) {
			stockComeEnterService.addStockComeEnter(stockComeEnter);
		}
		return JsonUtil.success();
	}

	// 商品锁定的修改
	@RequestMapping("updateStockStatus")
	public String updateStockStatus(String id, String status) {

		stockService.updateStockStatus(id, status);
		return JsonUtil.success();
	}

}
